layui.define(['jquery','layer','util'],function(exports){
	var $ = layui.jquery;
	var layer = layui.layer;
	var util =  layui.util;
	var index;
	var socket = new WebSocket('ws://'+baseUrl+'/websocket/'+sid);
	socket.onopen = function() {
		layer.msg("websocket已连接")
		//socket.send("这是来自客户端的消息" + location.href + new Date());
	};
	socket.onmessage = function(msg) {
		$("#logAppend").append(msg.data+"<br><br>");
        var scrollDom = document.getElementById('logScroll');
        if(scrollDom){
			scrollDom.scrollTop = scrollDom.scrollHeight;
		}
	};
	socket.onerror = function() {
		alert("Socket发生了错误");
	};
	//关闭事件
	socket.onclose = function() {
		console.log("Socket已关闭");
	};
	var dhh  = {
		_ajax:function(url,data,method,callback){
			data = data ? data : {} ;
			method = method ? method : 'get' ;
			$.ajax({
				url:url,
				data:data,
				type:method,
				beforeSend: function (XMLHttpRequest) {
					index = layer.load();
					// XMLHttpRequest.setRequestHeader("token", sessionStorage.getItem(consts.common.token));
				},
				success:function(response){
					callback(response);
				},
				error:function(e,x){
					console.log(e);
					console.log(x)
				},
				complete:function(){
					layer.close(index);
				}
			});
		},
		post:function(url,data,callback){
			dhh._ajax(url,data,'post',function(response){
				callback(response);
			});
		},
		get:function(url,data){
			dhh._ajax(url,data,'get',function(response){
				callback(response);
			});
		},
		//随机日期
		randomBatchNo:function (n) {
			if (n > 21) return null;
			var re =new RegExp("(\\d{" + n + "})(\\.|$)");
			return (Array(n - 1).join(0) + Math.pow(10, n) * Math.random()).match(re)[1]
		}
	};
	
	exports('dhh',dhh);
});